package com.taotao.search.service.impl;

import org.apache.solr.client.solrj.SolrQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.taotao.common.SearchResult;
import com.taotao.search.dao.impl.SearchDaoImpl;
import com.taotao.search.service.SearchService;
/**
 * 搜索系统的业务逻辑层接口实现类，将所有SearchItem导入到索引库中
 */
@Service
@Transactional
public class SearchServiceImpl implements SearchService {


	//注入SearchDao
	@Autowired
	private SearchDaoImpl searchDaoImpl;


	/**
	 * 通过query查询索引库数据：实际业务层查询数据（业务逻辑）
	 */
	@Override
	public SearchResult searchItemByQuery(String queryString,Integer page,Integer rows) throws Exception {
		//创建查询条件SolrQuery
		SolrQuery query = new SolrQuery();
		query.setQuery(queryString);
		//设置分页条件（start ,rows）
		if(page<1){
			page=1;
		}
		query.setStart((page-1)*rows);
		query.setRows(rows);
		//设置默认域（df）
		query.set("df","item_title");
		//设置高亮显示域
		//开启高亮
		query.setHighlight(true);
		query.addHighlightField("item_title");
		query.setHighlightSimplePre("<em style=\'color=red\'>");
		query.setHighlightSimplePost("</em>");
		//需要计算总页数【总页数=总记录数/每页显示数】
		//调用dao层查询数据
		SearchResult result = searchDaoImpl.searchItemBySolr(query);
		long recordCount = result.getRecordCount();
		long pageCount = (long) Math.ceil(recordCount/rows);
		//封装到SearchResult
		result.setPageCount(pageCount);
		
		//返回SearchResult(此时三个参数已全部封装)
		return result;
	}
}
